package com.chint.largeData;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.RelativeLayout;
import android.widget.Toast;
import cn.jpush.android.api.JPushInterface;

import com.chint.largeData.Common.Constant;
import com.chint.largeData.fragments.EditLockFragment;
import com.chint.largeData.fragments.ShowLockFragment;
import com.chint.largeData.tools.SharePreferencesUtil;

public class MainActivity extends Activity {
	RelativeLayout mRoot;
	String mLockCode;
	Fragment mEditLockCodeFrag,mShowLockCodeFrag;
	long mTime = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initData();
        initView();
        Log.e("LXL", "MainActivity onCreate");
    }
	private void initData() {
		mLockCode = SharePreferencesUtil.getInstance(this).getValue(Constant.SP_KEY.LOCK_CODE);
	}
	private void initView() {
		mEditLockCodeFrag = new EditLockFragment();
		mShowLockCodeFrag = new ShowLockFragment();
		Fragment frag = null;
		mRoot = (RelativeLayout) findViewById(R.id.root);
		if(TextUtils.isEmpty(mLockCode)){
			frag = mEditLockCodeFrag;
		}else{
			frag = mShowLockCodeFrag;
		}
		FragmentManager fragManager = getFragmentManager();
		FragmentTransaction transaction = fragManager.beginTransaction();
		transaction.replace(R.id.root, frag);
		transaction.addToBackStack(null);
		transaction.commit();
	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		 long time2 = System.currentTimeMillis();
		 if(time2-mTime>800){
			 Toast.makeText(this, "再按一次返回键退出程序", Toast.LENGTH_SHORT).show();
		 }else{
			 setResult(-1);
			 Log.e("LXL", "finish MainActiivyt");
			 finish();
//			 System.exit(0);
		 }
		 mTime = time2;
		return true;
	}
	public void showLockFrag() {
		FragmentManager fragManager = getFragmentManager();
		FragmentTransaction transaction = fragManager.beginTransaction();
		transaction.replace(R.id.root, mShowLockCodeFrag);
		transaction.addToBackStack(null);
		transaction.commit();
		
	}
	 @Override
		protected void onResume() {
			super.onResume();
			JPushInterface.onResume(this);
		}


		@Override
		protected void onPause() {
			super.onPause();
			JPushInterface.onPause(this);
		}
}
